<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>

</head>
<body>

<div>
    <c:choose>
        <c:when test="${empty sessionScope.loginUser}">
            <a href="${pageContext.request.contextPath}/html/login.jsp">登录</a>   注册
        </c:when>
        <c:otherwise>
            欢迎${sessionScope.loginUser.nickname} 进入系统。   <a href="/gwc?userId=${sessionScope.loginUser.id}">购物车</a>   个人中心
            <a href="${pageContext.request.contextPath}/html/logout.jsp">退出</a>
        </c:otherwise>
    </c:choose>


</div>
<div style="margin: 0px auto;width: 1000px;border: 1px solid lavender;overflow: hidden">
        <div id="bookImg" class="bookInfo"  style="width:300px;height: 300px;border: 1px solid lavender;float:left;margin: 10px">
                <img src="${pageContext.request.contextPath}${requestScope.bookInfo.produceImgUrl}" width="300px" height="300px">
        </div>
        <div id="content">
                  书籍名称:${requestScope.bookInfo.productName}<br>
                 价格：${requestScope.bookInfo.price}<Br>
                 作者：${requestScope.bookInfo.author}<br>
            。。。。。
            <input type="text" name="nums" size="1" value="1" max-nums="${requestScope.bookInfo.numbers}"> <button id="btn01">加入购物车</button>


        </div>

</div>
<script type="text/javascript">
    window.onload = function () {
        document.querySelector("#btn01").onclick = function () {
            //1.判断是否登录
            <c:if test="${empty sessionScope.loginUser}">
                    alert("请登录。。");
                    window.location.href="${pageContext.request.contextPath}/html/login.jsp";
            </c:if>

            //异步请求
            var req = new XMLHttpRequest();
            var url ="${pageContext.request.contextPath}/shoppingCart"
            var method = "POST";
            req.open(method,url);
            //如果使用send传递参数，则必须设置请求头的上下文类型
            req.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");


            //需要传递的参数有
            var param = "methodName=addCart&productId=${requestScope.bookInfo.id}&nums="+document.getElementsByName("nums")[0].value;


            req.send(param);
            
            req.onreadystatechange = function () {
                if(req.readyState ==4){
                    if(req.status ==200 || req.status ==304){
                            if(req.responseText=="true"){
                                    alert("添加成功");
                            }else {
                                    alert("条件失败");
                            }

                    }

                }

            }


        }
    }
    
    
</script>


</body>
</html>
